<template>
  <div class="rightBodyContent">
    <div class="rightBodyContent-in">
      <div class="tab-table-line mb-10">
        <el-tabs v-model="activeName" type="card">
          <el-tab-pane :label="item.name" :name="item.id" v-for="item in tabList" :key="item.id"></el-tab-pane>
        </el-tabs>
        <div class="excelDownload">
          <el-button type="primary" size="small" @click="itemVisible=true" v-if="activeName==1">
            设置行政陪餐人员
          </el-button>
          <el-button class="ml-10" type="primary" size="small" @click="moduleTypeVisible=true" v-if="activeName==1"
                     plain>陪餐模式
          </el-button>
        </div>
      </div>
      <div class="toplineBox mb-10">
        <div class="toplineBox-left">
          <el-select placeholder="学年" v-model="formData.semester_id" size="small"
                     class="w-140 mr-10">
            <el-option :label="item.year" :value="item.id" v-for="(item, index) in yearList"
                       :key="index"></el-option>
          </el-select>
          <el-select placeholder="周" v-model="formData.week" size="small"
                     class="w-140 mr-10">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in weekList"
                       :key="index"></el-option>
          </el-select>
          <el-select placeholder="类型" v-model="formData.style" size="small"
                     class="w-140 mr-10">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in styleList"
                       :key="index"></el-option>
          </el-select>
          <el-button type="primary" size="small" @click="searchFn(1)"><i class="el-icon-search"></i>查询</el-button>
          <el-button size="small" @click="searchFn(2)"><i class="el-icon-refresh"></i>重置</el-button>
          <downexcel type="dishWeekAll" :option="formData" v-if="activeName==1"></downexcel>
          <downexcel type="dishWeek" :option="formData" v-if="activeName==1"></downexcel>
          <downexcel type="dishWeekParents" :option="formData" v-if="activeName==2"
                     :excelName="excelNameFn()"></downexcel>
        </div>
      </div>
      <div class="table-title">
        <span>{{ scoolTime ? scoolTime.year : '--' }}学年</span>/
        <span>{{ scoolTime && scoolTime.type == 1 ? '下' : '上' }}学期</span>/
        <span>第{{ weekNum }}周</span>
      </div>
      <div v-if="Number(activeName)==1">
        <weekTable :styleType="formData.style" :foodDay="foodDay" :foodType="foodType" :weekName="weekName" :weekContent="weekContent"
                   @showWeekFn="showWeekFn" v-if="foodModel==1" :dish_type="dish_type" @getIndexFn="getIndexFn"
                   @dishdelete="dishdelete"></weekTable>
        <weekTableModel2 :foodDay="foodDay" :foodType="foodType" :weekName="weekName" :weekContent="weekContent"
                         @showWeekFn="showWeekFn" v-if="foodModel==2" :dish_type="dish_type" @getIndexFn="getIndexFn"
                         @dishdelete="dishdelete"></weekTableModel2>
      </div>

      <parentsTable :foodDay="foodDay" :foodType="foodType" :weekName="weekName" :weekContent="weekContent"
                    @showWeekFn="showWeekFn" v-if="Number(activeName)==2"></parentsTable>
    </div>
    <addFoodTest :visible.sync="itemVisible"></addFoodTest>
    <addWeekEdit :visible.sync="weekVisible" :week="formData.week" :semester_id="formData.semester_id"
                 :weekContent="weekContent" @getInit="getInit"></addWeekEdit>
    <moduleTypeChoose :visible.sync="moduleTypeVisible" @getNewModle="getNewModle"></moduleTypeChoose>
  </div>
</template>

<script>
import addFoodTest from "./addFoodTest";
import addWeekEdit from "./addWeekEdit";
import {weekList} from '@/utils/util.js'
import {mapActions} from "vuex";
import {yearDay, yeartime} from '@/utils/util'
import weekTable from "./weekTable";
import parentsTable from "./parentsTable";
import {dish_comment_parent} from "@/api/admin/food"
import moduleTypeChoose from "./moduleTypeChoose";
import {templateDetail, dish_comment_delete} from '@/api/admin/food.js'
import weekTableModel2 from "./weekTableModel2";

export default {
  components: {
    addFoodTest,
    addWeekEdit,
    weekTable,
    parentsTable,
    moduleTypeChoose,
    weekTableModel2
  },
  data() {
    return {
      itemVisible: false,
      weekVisible: false,
      moduleTypeVisible: false,
      tabList: [
        {
          name: '行政陪餐',
          id: '1'
        },
        {
          name: '家长陪餐',
          id: '2'
        },
      ],
      activeName: '1',
      formData: {
        p: 0,// 	整型 	必填 	1 	- 	页码0全部
        week: '',// 	整型 	必填 	1 	- 	周
        semester_id: '',// 	整型 	必填 	1 	- 	学期
        style: 2
      },
      weekList: weekList,
      scoolTime: null,
      weekNum: 0,
      yearList: [],
      foodDay: [],
      foodType: {},
      weekName: {
        1: '星期一',
        2: '星期二',
        3: '星期三',
        4: '星期四',
        5: '星期五',
        6: '星期六',
        7: '星期日',
      },
      defultValue: null,
      weekContent: '',
      foodModel: 0, // 陪餐模式
      dish_type: [],
      styleList: [{
        id: 1,
        name: '试餐'
      },
        {
          id: 2,
          name: '陪餐'
        }]
    }
  },
  watch: {
    activeName() {
      if (Number(this.activeName) === 1) {
        this.getIndexFn()
      } else {
        this.parentsdayListFn()
      }
    }
  },
  created() {
    this.getNewModle()
    this.dayListFn()
  },
  methods: {
    ...mapActions(['getSemester']),
    async getIndexFn() {
      let data = await this.$fetch(this.api.food.dinner_party.index, this.formData);
      console.log('请求的数据：', data)
      if (!data.code) {
        // 重组信息 必须有5天
        for (let item of data.dish_type) {
          if (item.id == 3) {
            this.foodType[4] = item.name
          } else if (item.id == 4) {
            this.foodType[3] = item.name
          } else {
            this.foodType[item.id] = item.name
          }
        }
        this.dish_type = JSON.parse(JSON.stringify(data.dish_type))
        console.log('this.dish_type', this.dish_type)
        let list = {};
        for (let item of data.list) {
          let a = list[item.day] || {};
          // item.create_time = yeartime(item.create_time)
          if (item.create_time) {
            let a_ = item.create_time.split(':')
            a_.splice(2, 1)
            item.create_time = a_.join(':')
          }
          if (item.eat_time) {
            let b_ = item.eat_time.split(':')
            b_.splice(2, 1)
            item.eat_time = b_.join(':')
          }
          a[item.type] = item;
          list[item.day] = a;
        }
        console.log('list:', list)
        let b = [];
        for (let i in list) {
          let c = {
            time: yearDay(i)
            // time: yearDay(i), list: Object.keys(list[i]).map(function (index) {
            //   return list[i][index]
            // })
          }
          let d_ = []
          for (let item of data.dish_type) {
            d_.push(0)
          }
          let e_ = JSON.parse(JSON.stringify(list[i]))
          for (let index_ in e_) {
            if (index_ == 3) {
              d_[3] = e_[index_]
            } else if (index_ == 4) {
              d_[2] = e_[index_]
            } else {
              d_[index_ - 1] = e_[index_]
            }
          }
          c.list = d_
          c.dayTimeState = false
          for (let n of d_) {
            if (n) {
              c.dayTimeState = true
            }
          }

          for (let n in list[i]) {
            c.weekName = Number(list[i][n].weeks)
          }
          b.push(c)
        }
        this.weekContent = data.week ? data.week.content : ''
        this.foodDay = JSON.parse(JSON.stringify(b))
        console.log('最后的数据：', this.foodDay)
      }
      console.log('data:', data)
    },
    parentsdayListFn() {
      dish_comment_parent(this.formData).then(res => {
        console.log('parants_res:', res)
        // let list = {};
        // for (let item of res.list) {
        //   let a = list[item.day] || {};
        //   // item.create_time = yeartime(item.create_time)
        //   if (item.create_time) {
        //     let a_ = item.create_time.split(':')
        //     a_.splice(2, 1)
        //     item.create_time = a_.join(':')
        //   }
        //   if (item.eat_time) {
        //     let b_ = item.eat_time.split(':')
        //     b_.splice(2, 1)
        //     item.eat_time = b_.join(':')
        //   }
        //   a[item.type] = item;
        //   list[item.day] = a;
        // }
        // console.log('list:', list)
        // let b = [];
        // for (let i in list) {
        //   let c = {
        //     time: yearDay(i)
        //     // time: yearDay(i), list: Object.keys(list[i]).map(function (index) {
        //     //   return list[i][index]
        //     // })
        //   }
        //   let d_ = [0, 0, 0]
        //   let e_ = JSON.parse(JSON.stringify(list[i]))
        //   for (let index_ in e_) {
        //     d_[index_ - 1] = e_[index_]
        //   }
        //   c.list = d_
        //   c.dayTimeState = false
        //   for(let n of d_){
        //     if(n){
        //       c.dayTimeState = true
        //     }
        //   }
        //
        //   for (let n in list[i]) {
        //     c.weekName = Number(list[i][n].weeks)
        //   }
        //   b.push(c)
        // }
        for (let item of res.list) {
          let foodList = []
          for (let food of item.release_dish_ids) {
            foodList.push(food.name)
          }
          item.foodList = foodList.join(',')
        }
        // this.weekContent = res.week ? res.week.content : ''
        this.foodDay = JSON.parse(JSON.stringify(res.list))
        console.log('数据内容', this.foodDay)
      })
    },
    dayListFn() {
      this.getSemester().then(res => {
        if (!res.code) {
          this.scoolTime = res.vo;
          this.weekNum = res.week;
          this.defultValue = res
          this.formData.semester_id = res.vo.id
          this.formData.week = res.week
          if (Number(this.activeName) === 1) {
            this.getIndexFn()
          } else {
            this.parentsdayListFn()
          }
        }
      });
      this.$fetch(this.api.setting.semesterIndex, {}).then((response) => {
        for (let i in response.list) {
          if (response.list[i].type === 1) {
            response.list[i].year = response.list[i].year + ' 下学期'
          } else {
            response.list[i].year = response.list[i].year + ' 上学期'
          }
        }
        this.yearList = response.list
      }).catch((err) => {
        console.log(err)
      })
    },
    searchFn(option) {
      if (option === 1) {
        console.log('内容：', this.formData)
        for (let item of this.yearList) {
          if (Number(this.formData.semester_id) === Number(item.id)) {
            this.scoolTime = JSON.parse(JSON.stringify({
              year: item.year.split(' ')[0],
              type: item.type
            }))
          }
        }
        this.weekNum = this.formData.week
        if (Number(this.activeName) === 1) {
          this.getIndexFn()
        } else {
          this.parentsdayListFn()
        }
      } else {
        this.scoolTime = this.defultValue.vo;
        this.formData.semester_id = this.defultValue.vo.id
        this.formData.week = this.defultValue.week
        if (Number(this.activeName) === 1) {
          this.getIndexFn()
        } else {
          this.parentsdayListFn()
        }
      }
    },
    getInit(option) {
      this.weekContent = option
    },
    showWeekFn() {
      console.log('000000==>')
      this.weekVisible = true
    },
    excelNameFn() {
      let string_ = ''
      for (let item of this.yearList) {
        if (item.id === this.formData.semester_id) {
          string_ = string_ + item.year
        }
      }
      for (let item of this.weekList) {
        if (item.id === this.formData.week) {
          string_ = string_ + item.name
        }
      }
      return string_
    },
    getNewModle() {
      templateDetail({id: 1}).then(res => {
        this.foodModel = res.vo.type
      })
    },
    dishdelete(option) {
      console.log('option:', option)
      let that = this
      this.$confirm(`删除【${yearDay(option.day)}】【${option.user ? option.user.realname : '无'}】记录, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        dish_comment_delete({id: option.id}).then(res => {
          that.getIndexFn()
        })
      }).catch(() => {
      });
    }
  },
  mounted() {
  }
}
</script>
<style scoped>
table {
  border-collapse: collapse;
}

table th {
  height: 50px;
}

table td {
  padding: 5px;
  text-align: center;
  font-size: 12px;
}

table td em {
  font-style: normal;
  font-size: 16px;
}

table td i {
  font-style: normal;
  font-size: 12px;
}

.table-title {
  margin-bottom: 10px;
}

.table-title span {
  font-weight: bold;
}

.td-01 {
  width: 80px;
}

.td-02 {
  width: 133px;
}

.td-40 {
  width: 40px
}

.td-50 {
  width: 50px
}

.tips-day-list {
  padding: 10px 0 20px;
  text-align: center;
}

.td-time {
  width: 80px
}
</style>
